
uniform sampler2D		alphaMap;
uniform sampler2D		bumpMap;
uniform sampler2D		refractionMap;

uniform vec2		xyScale;			// Passed in automatically
uniform vec2		potAdjust;			// Passed in automatically

varying vec2		alphaTexCoord;
varying vec4		bumpTexCoord;
varying float		deformScale;


void main (void){

	vec3		normalVec;
	vec3		refraction;
	vec2		texCoord;
	float		mask;

	// Load the mask from the alpha map
	mask = texture2D(alphaMap, alphaTexCoord).a;

	// Load the normal vector from the bump map
	normalVec = (texture2D(bumpMap, bumpTexCoord.st).rgb * 2.0 - 1.0) + (texture2D(bumpMap, bumpTexCoord.pq).rgb * 2.0 - 1.0);
	normalVec = normalize(normalVec);

	// Load the refraction map with a modified texcoord
	texCoord = (gl_FragCoord.xy * xyScale) + (normalVec.xy * deformScale * mask);
	texCoord = clamp(texCoord, 0.0, 1.0) * potAdjust;

	refraction = texture2D(refractionMap, texCoord).rgb;

	// Write the final color, and copy the mask to alpha
	gl_FragColor = vec4(refraction, mask);
}
